/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */
package com.yami.shop.live.platform.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.LiveInterfaceType;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.live.common.model.LiveRoom;
import com.yami.shop.live.common.param.LiveUsableNumParam;
import com.yami.shop.live.common.service.LiveLogService;
import com.yami.shop.live.common.service.LiveRoomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author lhd
 * @date 2020-08-05 08:53:17
 */
@RestController
@AllArgsConstructor
@RequestMapping("/live/liveRoom")
@Api(tags = "平台端直播间接口")
public class LiveRoomController {

    private final LiveRoomService liveRoomService;
    private final LiveLogService liveLogService;

    @GetMapping("/page")
    @ApiOperation(value = "分页查找直播间信息")
    public ServerResponseEntity<IPage<LiveRoom>> getLiveRoomPage(PageParam<LiveRoom> page, LiveRoom liveRoom) {
        return ServerResponseEntity.success(liveRoomService.page(page, new LambdaQueryWrapper<LiveRoom>()
                .like(StrUtil.isNotBlank(liveRoom.getName()), LiveRoom::getName, liveRoom.getName())
                .like(StrUtil.isNotBlank(liveRoom.getAnchorName()), LiveRoom::getAnchorName, liveRoom.getAnchorName())
                .eq(Objects.nonNull(liveRoom.getLiveStatus()), LiveRoom::getLiveStatus, liveRoom.getLiveStatus())
                .eq(Objects.nonNull(liveRoom.getRoomTop()), LiveRoom::getRoomTop, liveRoom.getRoomTop())
                .ge(Objects.nonNull(liveRoom.getStartTime()), LiveRoom::getStartTime, liveRoom.getStartTime())
                .le(Objects.nonNull(liveRoom.getStartTime()), LiveRoom::getStartTime, liveRoom.getEndTime())
                .orderByDesc(LiveRoom::getStartTime)
        ));
    }

    @GetMapping("/info/{id}")
    @ApiOperation(value = "通过id查询直播间商品库")
    @ApiImplicitParam(name = "id", value = "直播间id", required = true, dataType = "Long")
    public ServerResponseEntity<LiveRoom> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(liveRoomService.getById(id));
    }

    @PutMapping("/toTop/{id}")
    @PreAuthorize("@pms.hasPermission('live:liveRoom:update')")
    @ApiOperation(value = "通过id置顶直播间")
    @ApiImplicitParam(name = "id", value = "直播间id", required = true, dataType = "Long")
    public ServerResponseEntity<Boolean> toTopRoom(@PathVariable Long id) {
        LiveRoom liveRoom = liveRoomService.getById(id);
        int count = liveRoomService.count(new LambdaQueryWrapper<LiveRoom>().eq(LiveRoom::getRoomTop, 1));
        if (count >= Constant.MAX_TOP_NUM && Objects.equals(liveRoom.getRoomTop(), 0)) {
            // 置顶已达上限10个无法进行置顶，请先取消其余直播间的置顶
            throw new YamiShopBindException("yami.live.room.top.check");
        }
        liveRoom.setRoomTop(Objects.equals(liveRoom.getRoomTop(), 0) ? 1 : 0);
        return ServerResponseEntity.success(liveRoomService.updateById(liveRoom));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过id删除直播间商品库")
    @ApiImplicitParam(name = "id", value = "直播间id", required = true, dataType = "Long")
    public ServerResponseEntity<Boolean> removeById(@PathVariable Long id) throws WxErrorException {
        return ServerResponseEntity.success(liveRoomService.removeRoomById(id, Constant.PLATFORM_SHOP_ID));
    }

    @GetMapping("/getRemoveRoomNum")
    @ApiOperation(value = "查询商家今日剩余可用添加直播间次数")
    public ServerResponseEntity<LiveUsableNumParam> getRemoveRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.REMOVE_LIVE_ROOM);
        LiveUsableNumParam deleteLiveNum = liveLogService.getLiveNum(Constant.PLATFORM_SHOP_ID, LiveInterfaceType.REMOVE_LIVE_ROOM);
        liveNum.setDeleteShopNum(deleteLiveNum.getShopNum());
        liveNum.setDeleteTotalNum(deleteLiveNum.getTotalNum());
        return ServerResponseEntity.success(liveNum);
    }
}
